package com.itboye.lottery.activity.user;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.lottery.BuildConfig;
import com.itboye.lottery.base.BaseActivity;
import com.itboye.lottery.R;
import com.itboye.lottery.alert.ByAlert;
import com.itboye.lottery.bean.AppConfigBean;
import com.itboye.lottery.presenter.UserPresenter;
import com.itboye.lottery.retrofit.ResponseCallback;
import com.itboye.lottery.utils.Flavor;
import com.itboye.lottery.utils.PermissionListener;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class ContactKeFuActivity extends BaseActivity implements ResponseCallback {

    UserPresenter userPresenter = new UserPresenter(this);
    TextView tv_service_phone;
    private String phoneNum;
    ImageView iv_call;
    private String[] PERMISSIONS = new String[]{Manifest.permission.CALL_PHONE};

    @Override
    protected int getLayoutId() {
        return R.layout.activity_contact_ke_fu;
    }

    @Override
    protected void initData() {
        if (BuildConfig.app_type.equals("ttgc")) {
            iv_call.setVisibility(View.GONE);
        }
        userPresenter.getAppConfig();
        checkMyPermission();
    }

    boolean isGranted = true;

    private boolean checkMyPermission() {
        //判断系统版本
        if (Build.VERSION.SDK_INT >= 23) {
            requestRunPermisssion(PERMISSIONS, new PermissionListener() {
                @Override
                public void onGranted() {
                    isGranted = true;
                }

                @Override
                public void onDenied(List<String> deniedPermission) {
                    openAppDetails("检测到您拒绝了打电话的权限,请手动打开哦!");
                    isGranted = false;
                }
            });
        }
        return isGranted;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_call:
                call();
                break;
        }
    }

    @Override
    public void success(@NotNull Object o, @NotNull String requestSuccessType) {
        ArrayList<AppConfigBean> appConfigBeanArrayList = (ArrayList<AppConfigBean>) o;
        //客服电话
        phoneNum = appConfigBeanArrayList.get(2).getValue();
        tv_service_phone.setText(Html.fromHtml(new Flavor().getKefu()+ phoneNum));
    }

    public void call() {
        if (!TextUtils.isEmpty(phoneNum)) {
            diallPhone(phoneNum);
        }
    }

    @Override
    public void fail(int code, @NotNull Object msg, @NotNull String requestErrorType) {
        ByAlert.alert(msg);
    }

    /**
     * 拨打电话（跳转到拨号界面，用户手动点击拨打）
     *
     * @param phoneNum 电话号码
     */
    public void diallPhone(String phoneNum) {
        if (checkMyPermission()) {
            Intent intent = new Intent(Intent.ACTION_DIAL);
            Uri data = Uri.parse("tel:" + phoneNum);
            intent.setData(data);
            startActivity(intent);
        }
    }
}
